#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "parser.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //ui->graphicsView->setScene(scene);

    QLabel* label = new QLabel(this);
    label->setText("Right-click to add Tech Items and/or Dependancies");
    statusBar()->addWidget(label);
    setWindowTitle("Tech Item Editor");

    fileMenu = new QMenu("File");
    editMenu = new QMenu("Edit");
    viewMenu = new QMenu("View");
    helpMenu = new QMenu("Help");

    saveRsAct = new QAction("Save Resources", this);
    saveCsAct = new QAction("Save Categories", this);
    saveAsAct = new QAction("Save As", this);
    exportAct = new QAction("Export as PNG", this);
    exitAct = new QAction("Exit", this);

    insCategoryAct = new QAction("Insert/Edit/Delete Categories", this);
    insResourceAct = new QAction("Insert/Edit/Delete Resources", this);
    insDependancyAct = new QAction("Insert new Dependancy", this);
    delDependancyAct = new QAction("Delete Dependancy", this);
    editDependancyAct = new QAction("Edit Dependancy", this);
    insTechItemAct = new QAction("Insert new Tech Item", this);
    delTechItemAct = new QAction("Delete Tech Item", this);
    editTechItemAct = new QAction("Edit Tech Item", this);
    listItemsAct = new QAction("List all Tech Items", this);
    aboutAct = new QAction("About", this);

   menuBar()->addMenu(fileMenu);
   fileMenu->addAction(saveRsAct);
   fileMenu->addAction(saveCsAct);
   fileMenu->addAction(saveAsAct);
   fileMenu->addAction(exportAct);
   fileMenu->addAction(exitAct);

   menuBar()->addMenu(editMenu);
   editMenu->addAction(insCategoryAct);
   editMenu->addAction(insResourceAct);

   menuBar()->addMenu(viewMenu);
   viewMenu->addAction(listItemsAct);

   menuBar()->addMenu(helpMenu);
   helpMenu->addAction(aboutAct);

   ui->graphicsView->setContextMenuPolicy(Qt::CustomContextMenu);
   connect(ui->graphicsView, SIGNAL(customContextMenuRequested(const QPoint)), this, SLOT(contextMenu(const QPoint)));
   connect(aboutAct, SIGNAL(triggered()),this, SLOT(showAbout()));
   connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

   ItemHandler *itemHandler = new ItemHandler(this);
   Parser *parser = new Parser(itemHandler, this);
   parser->parse();
   delete parser;
   delete itemHandler;
}

MainWindow::~MainWindow()
{
    delete ui;
}


/*void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu menu(this);
    menu.addAction(insDependancyAct);
    menu.addAction(editDependancyAct);
    menu.addAction(delDependancyAct);

    menu.addAction( insTechItemAct);
    menu.addAction(editTechItemAct);
    menu.addAction(delTechItemAct);

    menu.exec(event->globalPos());

}
*/

void MainWindow::contextMenu(const QPoint& pos)
{
    QMenu* menu = new QMenu;
    menu->addAction(insDependancyAct);
    menu->addAction(editDependancyAct);
    menu->addAction(delDependancyAct);

    menu->addAction( insTechItemAct);
    menu->addAction(editTechItemAct);
    menu->addAction(delTechItemAct);
    menu->exec(ui->graphicsView->mapToGlobal(pos));

}


void MainWindow::showAbout()
{
    QMessageBox message;
    message.setText( "Blah blah blah\nBlah blah blah");
    message.exec();
}
